<?php
/**
 * Created by RisingSun.
 * User: MarkSpace
 * Date: 20220315
 * Time: 11:05
 * 账户模块
 */
namespace app\api\model;

use Mscore\Core\Redis;
use Mscore\Core\Lib\SequenceNumber;
use Mscore\Core\Util\Util;

class Account extends Model
{

    private $account = 'user_account';
    private $cert = 'user_cert';
    private $pertable = 'sys_rules';
    private $perroletable = 'sys_role_has';
    private $ident = 'sys_ident';
    private $exchange = 'settle_exchange';

    /**
     * 用户信息更新
     * @param array $updateuser
     * @param array $id
     */
    public function updateuser($data)
    {
        if(empty($data['aid'])){
            return false;
        };
        $info = $this->info(['aid'=>$data['aid']],['un_id','usercode','paypwd','header_url']);
        $paypwd = !empty($data['paypwd'])?Util::passwordHash($data['paypwd']):$info['paypwd'];
        $header_url = !empty($data['header_url']) ? $data['header_url'] : $info['header_url'];
        $un_id = !empty($info['un_id']) ? $info['un_id'] : SequenceNumber::unique_ID(26);
        $usercode = !empty($info['usercode']) ? $info['usercode'] : getRandStr(6);
        if ($data['header_url']) {
            @unlink(config('file.path') . $info['header_url']);
        }
        unset($info);
        $saveData = [
            'realname' => $data['realname'],
            'mobile' => $data['mobile'],
            // 'cityarea' => $data['cityarea'],
            'paypwd' => $paypwd,
            'header_url' => $header_url,
            'un_id' => $un_id,
            'usercode' => $usercode,
        ];
        Model::name($this->account)->where(['aid'=>$data['aid']])->update($saveData);
        return true;
    }

    /**
     * 账户信息
     * @param array $info
     * @param array $aid
     */
    public function info($where = '', $file = '*')
    {
        $info = Model::name($this->account)
            ->field($file)
            ->where($where)
            ->find();
        return $info;
    }

    /**
     * 认证信息
     * @param array $cert
     * @param array $aid
     */
    public function cert($where = '', $file = '*')
    {
        $info = Model::name($this->cert)
            ->field($file)
            ->where($where)
            ->find();
        return $info;
    }

    /**
     * 支持的幣種列表
     * @param array $currency
     * @param array $aid
     */
    public function currency($where = '', $file = '*')
    {
        $info = Model::name($this->exchange)
            ->field($file)
            ->where($where)
            ->select()
            ->toArray();
        return $info;
    }

    /**
     * 认证修改
     * @param array $updatecert
     * @param array $id
     */
    public function updatecert($data)
    {
        $certificate=[ 'IDcard'=>$data['IDcard'], 'name'=>$data['name']];
        $cert_url=['IDcardF'=>$data['IDcardF'],'IDcardB'=>$data['IDcardB']];
        $cert = Model::name($this->cert)->where(' aid=' . $data['aid'])->count('uid');
        $saveData = [
                    'aid' => $data['aid'],
                    'pid' => $data['pid'],
                    'certificate' => json_encode($certificate,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),
                    'cert_url'=> json_encode($cert_url,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),
                    'status' =>2
                ];
        switch (true) {
            case $cert>0:                
                Model::name($this->cert)->where(' aid=' . $data['aid'])->update($saveData);
                return true;
                break;
            default:
                $uid=Model::name($this->cert)->insertGetId($saveData);
                Model::name($this->account)->where(' aid=' . $data['aid'])->update(['uid'=>$uid]);
                return true;
                break;
        }
        
    }
    
    /**
     * 修改姓名
     * @param array $updatename
     * @param array $id
     */
    public function updatename($data)
    {
        $saveData = [
            'realname' => $data['realname'],
        ];
        switch (true) {
            case $data['aid']>0:                
                Model::name($this->account)->where(' aid=' . $data['aid'])->update($saveData);
                return true;
                break;
            default:
                return false;
                break;
        }
        
    }

    /**
     * 账户保存
     * @param array $save
     * @param array $id
     */
    public function save($data)
    {

        $user = [];
        switch (true) {
            case !empty($data['aid']):
                $info = $this->info($user,['usercode','password','showpwd','header_url']);
                $password = !empty($data['password']) ? Util::passwordHash($data['password']) : $info['password'];
                $header_url = !empty($data['imgOne']) ? $data['imgOne'] : $info['header_url'];
                $un_id = !empty($info['un_id']) ? $info['un_id'] : SequenceNumber::unique_ID(26);
                $usercode = !empty($info['usercode']) ? $info['usercode'] : getRandStr(6);
                if ($data['imgOne'] && !config('oss.oss')) {
                    @unlink(config('file.path') . $info['header_url']);
                }
                $showpwd=!empty($info['showpwd'])?json_decode($info['showpwd'],true):['password'=>'','paypwd'=>''];
                $showpwd['password'] = !empty($data['password']) ? Util::phpEncode($data['password']) : $showpwd['password'];
                unset($user);
                break;
            default:
                $password = $data['password'];
                $header_url = $data['imgOne'];
                $showpwd=['password'=>'','paypwd'=>''];
                $showpwd['password'] = !empty($data['password']) ? Util::phpEncode($data['password']) :'';
                $un_id = SequenceNumber::unique_ID(26);
                $usercode = getRandStr(6);
                break;
        }
        $saveData = [
            'username' => $data['username'],
            'password' => $password,
            'showpwd'=>json_encode($showpwd,JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE),
            'realname' => $data['realname'],
            'mobile' => $data['mobile'],
            'email' => $data['email'],
            'grade' => $data['grade'],
            'ranges' => $data['ranges'],
            'roletype' => $data['roletype'],
            'status' => $data['status'],
            'header_url' => $header_url,
            'pid' => $data['pid'],
            'un_id' => $un_id,
            'usercode' => $usercode,
        ];
        switch ($data['aid']) {
            case true:
                Model::name($this->account)->where(' aid=' . $data['aid'])->update($saveData);
                return true;
                break;
            default:
                Model::name($this->account)->insert($saveData);
                return true;
                break;
        }
        return false;
    }

    /**
     * 用户支付密码
     * @param array $updatepaypwd
     * @param array $id
     */
    public function updatepaypwd($data)
    {
        if(empty($data['aid'])){
            return false;
        };
        // $info = $this->info(' aid= '.$data['aid'],['usercode','password','paypwd','showpwd','header_url']);
        $info = Model::name($this->account)->field('showpwd')->where(['aid'=>$data['aid']])->find();
        $showpwd=!empty($info['showpwd'])?json_decode($info['showpwd'],true):['password'=>'','paypwd'=>''];
        $password = Util::passwordHash($data['password']);
        $showpwd['paypwd'] = !empty($data['password']) ? Util::phpEncode($data['password']) : $showpwd['paypwd'];
        $saveData = [
            'paypwd' => $password,
            'showpwd'=>json_encode($showpwd,JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
        ];
        Model::name($this->account)->where(['aid'=>$data['aid']])->update($saveData);
        return true;
    }

    /**
     * 修改密码
     */
    public function updatepwd($data)
    {
        if(empty($data['aid'])){
            return false;
        };
        $aid=$data['aid'];
        $info = Model::name($this->account)->field('showpwd')->where(['aid'=>$aid])->find();
        $showpwd=!empty($info['showpwd'])?json_decode($info['showpwd'],true):['password'=>'','paypwd'=>''];
        $password = Util::passwordHash($data['newpassword']);
        $showpwd['password'] = !empty($data['newpassword']) ? Util::phpEncode($data['newpassword']) : $showpwd['password'];
        $data = [
            'password' => $password,
            'showpwd'=>json_encode($showpwd,JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
        ];
        Model::name($this->account)->where(' aid=' . $aid)->update($data);
        return true;
    }

    /**
     * 添加银行账户
     */
    public function updatebank($params)
    {
        $bank=[];
        $data = [
            'bank_name' => $params['bank_name'],
            'bank_account' => $params['bank_account'],
            'deposit_branch' => $params['deposit_branch'],
            'deposit_name' => $params['deposit_name'],
            'currency' => $params['currency'],
            'fileurl' => $params['fileurl'],
            'status'=>$params['status']
        ];
        $pid=Model::name($this->account)->where(' aid=' . $params['aid'])->value('pid');
        $uid=Model::name($this->cert)->where(' aid=' . $params['aid'])->count('uid');
        switch (true) {
            case $uid>0:
                $bank=Model::name($this->cert)->where(' aid=' . $params['aid'])->value('bankaccount');
                $bank=!empty($bank)&&isJson($bank)?json_decode($bank,true):[];
                if(count($bank)>=$params['bankaccsum']){ return false ;}
                $bank[]=$data;
                Model::name($this->cert)->where(' aid=' . $params['aid'])->update(['pid'=>$pid, 'bankaccount'=>json_encode($bank,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),'updatetime'=>date('Y-m-d H:i:s',time())]);
                break;
            default:
                $bank[]=$data;
                 Model::name($this->cert)->insert(['aid'=>$params['aid'], 'pid'=>$pid, 'bankaccount'=>json_encode($bank,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),'updatetime'=>date('Y-m-d H:i:s',time())]);
                break;
        }
        return true;
    }

    /**
     * 账户信息
     * @param array $info
     * @param array $aid
     */
    public function userlist($where = '', $page = 1, $page_size = 10, $file = '*')
    {
        //部门数据
        $user['data'] = Model::name($this->account)
            ->field($file)
            ->where($where)
            ->limit(($page - 1) * $page_size, $page_size)
            ->order('amount desc')
            ->select()
            ->toArray();
        //数据总数
        $user['total'] = Model::name($this->account)
            ->where($where)
            ->count('aid');
        return $user;
    }

}